package com.itheima;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.junit.jupiter.api.Test;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class testGenJwt {
    @Test
    public void testGenJwt1(){

        Map<String,Object> claims = new HashMap<>();
        claims.put("id",10);
        claims.put("username","itheima");

        String jwt = Jwts.builder().signWith(SignatureAlgorithm.HS256, "secret")
                .addClaims(claims)
                //有效时间
                .setExpiration(new Date(System.currentTimeMillis() + 12 * 3600 *1000))
                .compact();

        System.out.println(jwt);
    }


    @Test
    public void testParseJwt(){
        Claims claims = Jwts.parser()
                //指定签名密钥
                .setSigningKey("secret")
                .parseClaimsJws("eyJhbGciOiJIUzI1NiJ9.eyJpZCI6MTAsInVzZXJuYW1lIjoiaXRoZWltYSIsImV4cCI6MTc0OTE1MTYwNn0.u4t7iiwBndSP9bqrOjFW5mr-5TCiJdDPc7jrUpx2FXI")
                .getBody();

        System.out.println(claims);
    }
}
